package com.distributeredis.redis_springboot.redisdistribute.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class TestRedis {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisService redisService;

    /**
     * 操作String类型的数据
     */
    @PostMapping("/testString")
    public void testString() {
        // 存值
        redisTemplate.opsForValue().set("city","beijing");
        // 取值
        String value = redisTemplate.opsForValue().get("city");
        System.out.println(value);

        // 存值，同时设置过期时间
        redisTemplate.opsForValue().set("key","value",10L, TimeUnit.MINUTES);

        // 存值，如果存在则不执行任何操作
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("key", "value");
        System.out.println(aBoolean);
    }

    /**
     * 操作hash数据类型
     */
    @PostMapping("/testHash")
    public void testHash() {
        HashOperations hashOperations = redisTemplate.opsForHash();
        // 存值
        hashOperations.put("002","name","xiaoming");
        hashOperations.put("002","age","18");
        hashOperations.put("002","address","bj");

        // 取值
        String age = (String) hashOperations.get("002", "age");
        System.out.println(age);

        // 获取hash结构中的所有字段
        Set keys = hashOperations.keys("002");
        for (Object key : keys) {
            System.out.println(key);
        }
        // 获取hash结构中的所有值
        List values = hashOperations.values("002");
        for (Object value : values) {
            System.out.println(value);
        }
    }

    /**
     * 测试工具类
     */
    @PostMapping("/testRedisUtil")
    public void testRedisUtil() {
        PeopleUser p = new PeopleUser("小明",19,"河南");
        redisService.setCacheObject("user",p,10L,TimeUnit.MINUTES);
        Boolean flag = redisService.hasKey("user");
        if(flag) {
            Object user = redisService.getCacheObject("user");
            System.out.println(user);
        }
    }

    /**
     * 缓存集合对象
     */
    @PostMapping("/testRedisList")
    public void testRedisList() {
        PeopleUser p = new PeopleUser("小明",19,"河南");
        PeopleUser p1 = new PeopleUser("小明",19,"河南");
        PeopleUser p2 = new PeopleUser("小明",19,"河南");
        List<PeopleUser> list = new ArrayList<>();
        list.add(p);
        list.add(p1);
        list.add(p2);

        long count = redisService.setCacheList("list", list);
        Boolean flag = redisService.hasKey("list");
        if(flag) {
            Object userList = redisService.getCacheList("list");
            System.out.println(userList);
        }
    }
}
